#include<iostream>
#include<unistd.h>
#include<string.h>
#include<string>
#include<assert.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
char* argv[100];
char* genv[100];
int grgc,sz=200;
char* GetHome(){
    char* home=getenv("HOME");
    return home;
}
char* GetPwd(){
    char s[100];
    char* pwd=getcwd(s,100);
    return pwd;
}
char* GetUser(){
    char* user=getenv("USER");
    return user;
}
char* GetHostName(){
    char* hs;
    hs=(char*)malloc(100);
    gethostname(hs,sizeof(hs));
    return hs;
}
void CommandPrompt(){
   //user1@hcss-ecs-1cd5:~/process2$ 
   printf("%s%s/%s",GetUser(),GetHostName(),GetPwd());
   fflush(stdout);
} 
bool CheckCommandPrompt(char* s,int len){
    memset(argv,0,sizeof(argv));
    int i=0;
    const char* sep=" ";
    s[len-1]='\0';//不这样会多一个换行符号
    argv[i]=strtok(s,sep);
    while(argv[i]){
        i++;
        argv[i]=strtok(nullptr,sep);
    }
    return true;
}
void debug(){
    for(int i=0;argv[i];i++){
        std::cout<<argv[i]<<std::endl;
    }
}
bool GetCommand(char* s){
    //std::cout<<2<<std::endl
    char* ts=fgets(s,sz,stdin);
    //std::cout<<ts<<std::endl;
    if(ts==nullptr){
        //assert(0);
        return false;
    }
    int len=strlen(ts);
    if(len<=1){
        //assert(0);
        return false;
    }
    //std::cout<<ts<<std::endl;
    return true;
}
void InitGenv(){
    extern char** environ;
    //std::cout<<"ddddd"<<std::endl;
    int i=0;
    while(environ[i]){
        genv[i]=(char*)malloc(strlen(environ[i])+1);
        memset(genv[i],0,strlen(environ[i])+1);
        //std::cout<<environ[i]<<std::endl;
        strncpy(genv[i],environ[i],strlen(environ[i]));
        i++;
    }
    genv[i]=NULL;
}
void ExecuteCommand(){
    pid_t id=fork();
    if(id==0){
        execvpe(argv[0],argv,genv);
        exit(0);
    }
    else{
        int status1=0;
        pid_t wid=waitpid(id,&status1,0);
        return;
    }
}
int main(){
    char s[200];
    InitGenv();
    while(true){
            memset(s,0,sizeof(s));
            CommandPrompt();
            //scanf("%s",s);
            //GetCommand(s);
            if(!GetCommand(s)){
                continue;
            }
            //std::cout<<s<<std::endl;
            CheckCommandPrompt(s,strlen(s));
            //debug();
            ExecuteCommand();
           // sleep(10);
    }
    return 0;
}
